package cn.wizzer.app.web.commons.utils;

import cn.wizzer.app.sys.modules.services.SysSensitiveService;
import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.models.constant.ChatConstant;
import cn.wizzer.app.web.commons.ext.websocket.main.result.ChatResult;
import cn.wizzer.framework.util.JSONUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

@IocBean
public class NettyMsgUtil {

    private static final Log log = Logs.get();

    @Inject
    @Reference
    private SysSensitiveService sysSensitiveService;

    /**
     * 发送消息 客服？？
     *
     * @param channel
     * @param type
     * @param action
     * @param user
     * @param content
     */
    public void send(Channel channel, ChatResult.MessageTypeEnum type, ChatResult.ActionEnum action, User user, String content) {
        content = sysSensitiveService.filter(content);
        try {
            if (type == ChatResult.MessageTypeEnum.ACTION) {// 这边的user是只被操作的用户 场景移除添加用户等操作
                channel.writeAndFlush(new TextWebSocketFrame(JSONUtil.createJSONString(ChatConstant.PROP_CHAT_USER, JSONUtil.toJSONString(user), ChatConstant.ACTION, action.NAME, ChatConstant.TYPE, type.NAME, ChatConstant.CONTENT, content)));
            } else if (type == ChatResult.MessageTypeEnum.SYSTEM) { // 用户进群或离群判断，如果已经在群里，或者已经不在群里，系统发送消息通知
                channel.writeAndFlush(new TextWebSocketFrame(JSONUtil.createJSONString(ChatConstant.CONTENT, content, ChatConstant.ACTION, action.NAME, ChatConstant.TYPE, type.NAME)));
            } else {// 用户、客服 主动发送消息
                channel.writeAndFlush(new TextWebSocketFrame(JSONUtil.createJSONString(ChatConstant.CONTENT, content, ChatConstant.SENDER, JSONUtil.toJSONString(user), ChatConstant.ACTION, action.NAME, ChatConstant.TYPE, type.NAME)));
            }

        } catch (Exception e) {
            log.error("send error ：" + e.getMessage());
        }
    }

    /**
     * 群聊 ？？
     *
     * @param channels
     * @param type
     * @param action
     * @param user
     * @param content
     */
    public void send(ChannelGroup channels, ChatResult.MessageTypeEnum type, ChatResult.ActionEnum action, User user, String content) {
        content = sysSensitiveService.filter(content);
        try {
            if (type == ChatResult.MessageTypeEnum.ACTION) {
                channels.writeAndFlush(new TextWebSocketFrame(JSONUtil.createJSONString(ChatConstant.PROP_CHAT_USER, JSONUtil.toJSONString(user), ChatConstant.ACTION, action.NAME, ChatConstant.TYPE, type.NAME, ChatConstant.CONTENT, content)));
            } else if (type == ChatResult.MessageTypeEnum.SYSTEM) {
                channels.writeAndFlush(new TextWebSocketFrame(JSONUtil.createJSONString(ChatConstant.CONTENT, content, ChatConstant.ACTION, action.NAME, ChatConstant.TYPE, type.NAME)));
            } else {//
                channels.writeAndFlush(new TextWebSocketFrame(JSONUtil.createJSONString(ChatConstant.CONTENT, content, ChatConstant.SENDER, JSONUtil.toJSONString(user), ChatConstant.ACTION, action.NAME, ChatConstant.TYPE, type.NAME)));
            }

        } catch (Exception e) {

        }
    }

}
